package com.lcx.plugin.neteaseCloud.component.gd;

import com.lcx.plugin.neteaseCloud.constant.IconConstant;
import com.lcx.plugin.neteaseCloud.music.entity.MusicListEntity;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;
import java.util.Objects;

/**
 * 自定义树描述类,将树的每个节点设置成不同的图标
 *
 * @author daxiong
 * @date 2021/9/17 上午10:37
 */
public class MyDefaultTreeCellRenderer extends DefaultTreeCellRenderer {
    /**
     * ID
     */
    private static final long serialVersionUID = 1L;

    /**
     * 重写父类DefaultTreeCellRenderer的方法
     */
    @Override
    public Component getTreeCellRendererComponent(JTree tree, Object value,
                                                  boolean sel, boolean expanded, boolean leaf, int row,
                                                  boolean hasFocus) {

        // 执行父类原型操作
        super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,
                row, hasFocus);

        setText(value.toString());

        if (sel) {
            setForeground(getTextSelectionColor());
        } else {
            setForeground(getTextNonSelectionColor());
        }

        // 得到每个节点的TreeNode
        DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;

        MusicListEntity entity = (MusicListEntity) node.getUserObject();
        // 得到每个节点的text
        int level = entity.getLevel();

        // 设置为透明
        this.setOpaque(false);

        // 判断是哪个层级对应的值（这里如果节点传入的是一个实体,则可以根据实体里面的一个类型属性来显示对应的图标）
        if (Objects.equals(level, 0)) {
            this.setIcon(IconConstant.PERSON);
        } else if (Objects.equals(level, 1)) {
            this.setIcon(IconConstant.LIST);
        } else {
            this.setIcon(IconConstant.MUSIC_ITEM);
        }
        return this;
    }

}
